package cn.dnui_wwt524.dnuifood_16110100524.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import cn.dnui_wwt524.dnuifood_16110100524.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class CollectFragment extends Fragment {
    private TextView collect_shop,collect_food;
    private CollectFoodFragment collectFoodFragment;
    private CollectShopFragment collectShopFragment;
    private View view = null;
    protected FragmentManager childManager;
    protected FragmentTransaction transaction;

    public CollectFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view =  inflater.inflate(R.layout.fragment_collect, container, false);
        collect_shop =(TextView) view.findViewById(R.id.collect_shop);
        collect_food = (TextView) view.findViewById(R.id.collect_food);

        initViews();
        initEvents();
        return view;
    }

    private void initEvents() {
        collect_shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (collectShopFragment == null){
                    collectShopFragment = new CollectShopFragment();
                }
                replaceFragment(collectShopFragment);

                collect_shop.setSelected(true);
                collect_food.setSelected(false);
                collect_shop.setBackgroundResource(R.color.colorPrimaryDark);
                collect_food.setBackgroundResource(R.color.colorPrimary);
//                collect_shop.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
//                collect_food.setTextColor(getResources().getColor(R.color.colorPrimary));
            }
        });
        collect_food.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (collectFoodFragment == null){
                    collectFoodFragment = new CollectFoodFragment();
                }
                replaceFragment(collectFoodFragment);
                collect_shop.setSelected(false);
                collect_food.setSelected(true);
                collect_shop.setBackgroundResource(R.color.colorPrimary);
                collect_food.setBackgroundResource(R.color.colorPrimaryDark);
//                collect_shop.setTextColor(getResources().getColor(R.color.colorPrimary));
//                collect_food.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
            }
        });
    }

    private void initViews() {
        childManager = getChildFragmentManager();

        collect_shop.setSelected(true);
        collect_shop.setBackgroundResource(R.color.colorPrimaryDark);
        collect_food.setBackgroundResource(R.color.colorPrimary);
//        collect_shop.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
//        collect_food.setTextColor(getResources().getColor(R.color.colorPrimary));
    }
    @Override
    public void onResume() {
        super.onResume();
        collect_shop.setBackgroundResource(R.color.colorPrimaryDark);
        collect_food.setBackgroundResource(R.color.colorPrimary);
        if (collectShopFragment == null){
            collectShopFragment = new CollectShopFragment();
            addFragment(collectShopFragment);
        }else {
            replaceFragment(collectShopFragment);
        }
    }

    private void replaceFragment(Fragment fragment) {
        transaction=childManager.beginTransaction();
        transaction.replace(R.id.fragment_container,fragment);
        transaction.commit();
    }

    private void addFragment(Fragment fragment) {
        transaction=childManager.beginTransaction();
        transaction.add(R.id.fragment_container,fragment);
        transaction.commit();
    }

    @Override
    public void onAttach(Context context){
        super.onAttach(context);
    }
}
